package cn.lblbc.downloadlib.database;

import android.content.Context;

import androidx.room.Room;

/**
 * 数据库管理器
 */
public class DatabaseManager {
    private static volatile DatabaseManager sInstance;
    private DownloadDatabase mDatabase;

    private DatabaseManager() {
    }

    public static DatabaseManager getInstance() {
        if (sInstance == null) {
            synchronized (DatabaseManager.class) {
                if (sInstance == null) {
                    sInstance = new DatabaseManager();
                }
            }
        }
        return sInstance;
    }

    public void init(Context context) {
        if (mDatabase == null) {
            mDatabase = Room.databaseBuilder(context.getApplicationContext(),
                    DownloadDatabase.class, "download_database")
                    .build();
        }
    }

    public DownloadDatabase getDatabase() {
        if (mDatabase == null) {
            throw new IllegalStateException("Database not initialized. Call init() first.");
        }
        return mDatabase;
    }
}